//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.Provenance.Controls.Presenter
{
    using System;
    using Microsoft.Research.Provenance.Controls.View;
    using Microsoft.Research.ProvenanceCommon.Model;
    using Microsoft.Research.ProvenanceInterfaces;
    using Microsoft.Practices.Unity;
    using Microsoft.Research.ProvenanceCommon;
    using Microsoft.Research.DataLayer;
    using System.Globalization;
    using System.Collections.ObjectModel;
    using Microsoft.Research.Provenance.ProvenanceDataService;
    using System.Windows.Input;
    using Microsoft.Research.ScientificWorkflow.UIDesigner.WpfComposite;
    using System.ComponentModel;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;

    /// <summary>
    /// Presentation logic for displaying WorkFlowSummary.
    /// </summary>
    public class WorkflowSummaryPresenter : IDisposable, INotifyPropertyChanged
    {
        /// <summary>
        /// Get SummaryModel
        /// </summary>
        public ObservableCollection<WorkflowSummaryModel> SummaryModelCollection { get; private set; }

        /// <summary>
        /// Get WorkFlowSummaryView
        /// </summary>
        public WorkflowSummaryView View { get; private set; }

        /// <summary>
        /// The command displays Data products.
        /// </summary>
        public ICommand GetWorkflowSummary { get; private set; }

        /// <summary>
        /// This is to check whether the view is updated or not.
        /// </summary>
        public bool IsUpdated { get; private set; }

        /// <summary>
        /// This is used to set the expanding behaviour of the view.
        /// </summary>
        private bool isExpanded;

        /// <summary>
        /// Gets or sets the IsExpanded property.
        /// </summary>
        public bool IsExpanded
        {
            get
            {
                return this.isExpanded;
            }
            set
            {
                this.isExpanded = value;
                this.OnNotifyPropertyChanged("IsExpanded");
            }
        }

        /// <summary>
        /// The unity container instance.
        /// </summary>
        private IUnityContainer container;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="container">UnityContainer.</param>
        /// <param name="view">View related to WorkFlowSummary.</param>
        public WorkflowSummaryPresenter(IUnityContainer container, WorkflowSummaryView view)
        {
            this.container = container;
            this.View = view;
            this.SummaryModelCollection = new ObservableCollection<WorkflowSummaryModel>();

            this.GetWorkflowSummary = new DelegateCommand<object>(p => { this.OnGetWorkflowSummary(); });

            this.View.Presenter = this;

            this.IsUpdated = false;
        }

        /// <summary>
        /// This function is used to get workflow summary.
        /// </summary>
        private void OnGetWorkflowSummary()
        {
            try
            {
                WorkflowSummaryModel SummaryModel = this.container.Resolve<DataRetriever>().GetWorkflowSummary();
                if (SummaryModel != null)
                {
                    this.SummaryModelCollection.Add(SummaryModel);
                }
                this.IsUpdated = true;
            }
            catch (TridentCustomException ex)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(ex.Message);
            }
            catch (Exception exp)
            {
                TridentErrorHandler.HandleKnownExceptionsInUI(exp);
            }
        }

        #region IDisposable Members

        /// <summary>
        /// Clears the collection and View Object
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// nullify and Disposes the object 
        /// </summary>
        /// <param name="disposing"></param>
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.SummaryModelCollection = null;
                this.View = null;
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Event for property change.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raise property changed eventargs.
        /// </summary>
        /// <param name="changeProperty">Property anme.</param>
        private void OnNotifyPropertyChanged(string changeProperty)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(changeProperty));
            }
        }

        #endregion
    }
}
